home *** CD-ROM | disk | FTP | other *** search
/ Robotics & Artificial Int…3 (Professional Edition) / Robotics & Artificial Intelligence Tools 2003 (Professional Edition).iso / neural network tool and application / nsinstall.exe / data1.cab / Demos_Files / DLL / DECIMATE.C < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-08  |  2.9 KB  |  103 lines

  1. // Dynamic link library implementation of a decimator with running average filtering
  2.  
  3. #include "NSDLL.h"
  4.  
  5. #define    buffer(i, j)    bufferData->data[j+(i)*bufferData->length]
  6.  
  7. typedef struct {
  8.     int count;
  9.     int length;
  10.     NSFloat *data;
  11. } DecimatorData;
  12.  
  13. /***************************/
  14. /* Activation of component */
  15. __declspec(dllexport) BOOL performPrePost(
  16.     DLLData    *instance,    // Pointer to instance data (may be NULL)
  17.     NSFloat    *input,     // Pointer to the input data
  18.     NSFloat    *output,     // Pointer to the output data
  19.     int rows,         // Number of rows of data
  20.     int cols,         // Number of cols of data
  21.     BOOL preprocessor    // Flag to indicate whether this is a preprocessor or postprocessor
  22.     )
  23. {
  24.     int i, j;
  25.     int N = getIntParameter(instance, 2, 1);
  26.     DecimatorData *bufferData = getUserData(instance);
  27.     NSFloat result;
  28.  
  29.     if (N < 1)
  30.         N = 1;
  31.     for (i=N-1; i>0; i--)
  32.         for (j=0; j<bufferData->length; j++)
  33.             buffer(i,j) = buffer(i-1,j);
  34.     for (j=0; j<bufferData->length; j++)
  35.         buffer(0,j) = input[j];
  36.     if (++bufferData->count >= N) {
  37.         bufferData->count = 0;
  38.         if (!preprocessor)
  39.             for (j=0; j<bufferData->length; j++)
  40.                 output[j] = 0.0f;
  41.         for (j=0; j<bufferData->length; j++) {
  42.             result = 0.0f;
  43.             for (i=0; i<N; i++)
  44.                 result += buffer(i,j);
  45.             output[j] = result/N;
  46.         }
  47.         return TRUE;
  48.     }
  49.     return FALSE;
  50. }
  51.  
  52. /******************************************/
  53. /* Called every time the network is reset */
  54. __declspec(dllexport) void networkReset(
  55.     DLLData    *instance    // Pointer to instance data (may be NULL)
  56.     )
  57. {
  58.     int i, j;
  59.     int N = getIntParameter(instance, 2, 1);
  60.     DecimatorData *bufferData = getUserData(instance);
  61.     if (N < 1)
  62.         N = 1;
  63.  
  64.     for (i=0; i<N; i++)
  65.         for (j=0; j<bufferData->length; j++)
  66.             buffer(i,j) = 0.0f;
  67.     bufferData->count = 0;
  68. }
  69.  
  70. /******************************************/
  71. /* Management of instance data (OPTIONAL) */
  72. __declspec(dllexport) DLLData *allocPrePost(
  73.     DLLData    *oldInstance,    // Pointer to the last instance if reallocating
  74.     int *rows,         // Number of rows of output data, can be changed to reflect a diffenent number for the input data
  75.     int *cols,         // Number of cols of output data, can be changed to reflect a diffenent number for the input data
  76.     BOOL preprocessor    // Flag to indicate whether this is a preprocessor or postprocessor
  77.     )
  78. {
  79.     DLLData *instance = allocDLLInstance(oldInstance);
  80.     DecimatorData *bufferData = calloc(1,sizeof(DecimatorData));
  81.     int N;
  82.     setParameterName(instance, 2, 1, "N", TRUE);
  83.     setIntParameter(instance, 2, 1, 4, FALSE);
  84.     bufferData->length = *rows * *cols;
  85.     N = getIntParameter(instance, 2, 1);
  86.     if (N < 1)
  87.         N = 1;
  88.     bufferData->data = calloc(bufferData->length*N, sizeof(NSFloat));
  89.     setUserData(instance, bufferData);
  90.     return instance;
  91. }
  92.  
  93. __declspec(dllexport) void freePrePost(DLLData *instance)
  94. {
  95.     DecimatorData *bufferData = getUserData(instance);
  96.     if (bufferData) {
  97.         free(bufferData->data);
  98.         free(bufferData);
  99.     }
  100.     freeDLLInstance(instance);
  101. }
  102.  
  103.